package com.bsoft.gol.hcs.common.dao;


import com.bsoft.gol.hcs.common.entity.FusConsultAsk;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;

import java.util.Date;
import java.util.List;
import java.util.Map;


public abstract class FusConsultAskDAO extends HibernateSupportDelegateDAO<FusConsultAsk> {



    public FusConsultAsk getConsultInfoByDoctorId(final String doctorId, final  String consultMpiId) {
        HibernateStatelessResultAction<FusConsultAsk> action = new AbstractHibernateStatelessResultAction<FusConsultAsk>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append(" select fus_consult_ask_id as fusConsultAskId ,consult_id as consultId  ,finished_time as finishedTime, " +
                        " org_id as orgId from fus_consult_ask  where consult_mpi_id =:consultMpiId and  doctor_id=:doctorId ");

                sql.append(" order by fus_consult_ask_id  desc limit 1 ");
                Query query = ss.createSQLQuery(sql.toString());
                query.setParameter("consultMpiId", consultMpiId);
                query.setParameter("doctorId", doctorId);

                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                FusConsultAsk fusConsultAsk = new FusConsultAsk();
                List<Map> list = query.list();

                if (list.size() > 0) {
                    for (Map map : list) {
                        fusConsultAsk = ConversionUtils.convert(map, FusConsultAsk.class);
                        break;
                    }
                } else {
                    fusConsultAsk = null;
                }
                setResult(fusConsultAsk);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    @DAOMethod(sql = " update  FusConsultAsk set finishedTime=:finishedTime where fusConsultAskId =:fusConsultAskId")
    public abstract void updateById(@DAOParam("finishedTime") Date finishedTime, @DAOParam("fusConsultAskId") Long fusConsultAskId);

}
